/*
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import autoteam.Child;
import autoteam.tables.ChildEditorTableModel;

public class ChildEditor extends JDialog
{
    
    private static final long serialVersionUID = 4997576534107732677L;
    private static ChildEditor dialog = null;
    private static boolean val = true;
    ChildEditorTableModel childEditorModel = null;
    
    /**
     * Set up the dialog. The first argument can be null, but it really should
     * be a component in the dialog's controlling frame.
     */
    private static void initialize( JFrame frame, Child c )
    {
	dialog = new ChildEditor( frame, c );
    }
    
    /**
     * Show the initialized dialog. The first argument should be null if you
     * want the dialog to come up in the center of the screen. Otherwise, the
     * argument should be the component on top of which the dialog should
     * appear.
     */
    public static boolean showDialog( JFrame frame, Child _c )
    {
	if ( dialog == null )
	    initialize( frame, _c );
	else
	    dialog.childEditorModel.setChild( _c );
	
	dialog.setLocationRelativeTo( frame );
	dialog.setVisible( true );
	
	return val;
    }
    
    private ChildEditor( JFrame frame, Child _c )
    {
	super( frame, "ChildEditor", true );
	
	// buttons
	JButton cancelButton = new JButton( "Cancel" );
	final JButton okButton = new JButton( "OK" );
	cancelButton.addActionListener( new ActionListener( )
	{
	    public void actionPerformed( ActionEvent e )
	    {
		ChildEditor.dialog.setVisible( false );
		ChildEditor.val = false;
	    }
	} );
	okButton.addActionListener( new ActionListener( )
	{
	    public void actionPerformed( ActionEvent e )
	    {
		try
		{
		    childEditorModel.transfer( );
		    ChildEditor.dialog.setVisible( false );
		    ChildEditor.val = true;
		}
		catch ( java.text.ParseException ex )
		{
		    JOptionPane.showMessageDialog( dialog, ex.getMessage( ), "Date Error", JOptionPane.ERROR_MESSAGE,
						   null );
		}
	    }
	} );
	getRootPane( ).setDefaultButton( okButton );
	
	// the editing table
	
	childEditorModel = new ChildEditorTableModel( _c );
	final JTable table = new JTable( childEditorModel );
	table.setPreferredScrollableViewportSize( new Dimension( 500, 170 ) );
	
	JScrollPane tableScroller = new JScrollPane( table );
	tableScroller.setPreferredSize( new Dimension( 500, 180 ) );
	// XXX: Must do the following, too, or else the scroller thinks
	// XXX: it's taller than it is:
	tableScroller.setMinimumSize( new Dimension( 500, 180 ) );
	tableScroller.setAlignmentX( LEFT_ALIGNMENT );
	
	// Create a container so that we can add a title around
	// the scroll pane. Can't add a title directly to the
	// scroll pane because its background would be white.
	// Lay out the label and scroll pane from top to button.
	JPanel tablePane = new JPanel( );
	tablePane.setLayout( new BoxLayout( tablePane, BoxLayout.Y_AXIS ) );
	tablePane.add( Box.createRigidArea( new Dimension( 0, 5 ) ) );
	tablePane.add( tableScroller );
	tablePane.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
	
	// Lay out the buttons from left to right.
	JPanel buttonPane = new JPanel( );
	buttonPane.setLayout( new BoxLayout( buttonPane, BoxLayout.X_AXIS ) );
	buttonPane.setBorder( BorderFactory.createEmptyBorder( 0, 10, 10, 10 ) );
	buttonPane.add( Box.createHorizontalGlue( ) );
	buttonPane.add( okButton );
	buttonPane.add( Box.createRigidArea( new Dimension( 10, 0 ) ) );
	buttonPane.add( cancelButton );
	
	// Put everything together, using the content pane's BorderLayout.
	Container contentPane = getContentPane( );
	contentPane.add( tablePane, BorderLayout.CENTER );
	contentPane.add( buttonPane, BorderLayout.SOUTH );
	
	pack( );
	
    }
    
}
